package com.hymjweb.demo.hymj.main_manage.controller;

import com.alibaba.fastjson.JSON;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.main_manage.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;


/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.main_manage.controller
 * @Author: luoge
 * @CreateTime: 2021-02-04 09:17
 * @Description:
 */

@Controller
@RequestMapping("/main")
public class MainController extends BaseController {

    @Autowired
    private MainService mainService;

    @RequestMapping("/page")
    public String main() {

        Object globalCache = mainService.getCache();
        // 设置需要返回的缓存
        setRequestAttribute("globalCache", JSON.toJSON(globalCache));

        // 返回菜单，注意后面根据用户情况返回不同的菜单就在这个地方处理
        Object menuTree = mainService.getMenuTree();
        setRequestAttribute("menuTree", JSON.toJSON(menuTree));

        return "pages/main";
    }

    @RequestMapping("/showDepartmentOpenBill")
    @ResponseBody
    public Object showDepartmentOpenBill(@RequestBody(required = true) Map param) throws Exception {
        if(param==null || param.isEmpty()){
            throwException("分单统计参数错误!");
        }

        String startDate = DataTypeUtil.toStringObject(param.get("startDate"));
        String endDate = DataTypeUtil.toStringObject(param.get("endDate"));

        if(DataTypeUtil.isEmptyStr(startDate) || DataTypeUtil.isEmptyStr(endDate) ){
            endDate = DateUtil.getSysDateStr();
            startDate = DateUtil.dateToStr(DateUtil.getBeforeOrAfterDate(DateUtil.getSysDate(),-6));
        }

        BaseResultMap baseResultMap = new BaseResultMap();

        Object option = mainService.getDepartmentOpenBillEchartOption(startDate,endDate);

        baseResultMap.setOk(true);
        baseResultMap.setData(option);
        return baseResultMap;
    }


    @RequestMapping("/showDepartmentDailyOpenBill")
    @ResponseBody
    public Object showDepartmentDailyOpenBill(@RequestBody(required = true) Map param) throws Exception {
        if(param==null || param.isEmpty()){
            throwException("分单统计参数错误!");
        }

        String startDate = DataTypeUtil.toStringObject(param.get("startDate"));
        String endDate = DataTypeUtil.toStringObject(param.get("endDate"));

        if(DataTypeUtil.isEmptyStr(startDate) || DataTypeUtil.isEmptyStr(endDate) ){
            endDate = DateUtil.getSysDateStr();
            startDate = DateUtil.dateToStr(DateUtil.getBeforeOrAfterDate(DateUtil.getSysDate(),-6));
        }

        BaseResultMap baseResultMap = new BaseResultMap();

        Object option = mainService.getDepartmentDailyOpenBillEchartOption(startDate,endDate);

        baseResultMap.setOk(true);
        baseResultMap.setData(option);
        return baseResultMap;
    }


    @RequestMapping("/showFlightAirlinesOpenBill")
    @ResponseBody
    public Object showFlightAirlinesOpenBill(@RequestBody(required = true) Map param) throws Exception {
        if(param==null || param.isEmpty()){
            throwException("主单统计参数错误!");
        }

        String startDate = DataTypeUtil.toStringObject(param.get("startDate"));
        String endDate = DataTypeUtil.toStringObject(param.get("endDate"));

        if(DataTypeUtil.isEmptyStr(startDate) || DataTypeUtil.isEmptyStr(endDate) ){
            endDate = DateUtil.getSysDateStr();
            startDate = DateUtil.dateToStr(DateUtil.getBeforeOrAfterDate(DateUtil.getSysDate(),-6));
        }

        BaseResultMap baseResultMap = new BaseResultMap();

        Object option = mainService.showFlightAirlinesOpenBill(startDate,endDate);

        baseResultMap.setOk(true);
        baseResultMap.setData(option);
        return baseResultMap;
    }


    @RequestMapping("/showFlightAirportOpenBill")
    @ResponseBody
    public Object showFlightAirportOpenBill(@RequestBody(required = true) Map param) throws Exception {
        if(param==null || param.isEmpty()){
            throwException("主单统计参数错误!");
        }

        String startDate = DataTypeUtil.toStringObject(param.get("startDate"));
        String endDate = DataTypeUtil.toStringObject(param.get("endDate"));

        if(DataTypeUtil.isEmptyStr(startDate) || DataTypeUtil.isEmptyStr(endDate) ){
            endDate = DateUtil.getSysDateStr();
            startDate = DateUtil.dateToStr(DateUtil.getBeforeOrAfterDate(DateUtil.getSysDate(),-6));
        }

        BaseResultMap baseResultMap = new BaseResultMap();

        Object option = mainService.showFlightAirportOpenBill(startDate,endDate);

        baseResultMap.setOk(true);
        baseResultMap.setData(option);
        return baseResultMap;
    }


}
